package com.zang.tree;

import java.util.*;

/**
 * 二叉树的右视图
 *
 * @author ZVerify
 * @since 2022/10/23 11:18
 * @see <a href="https://leetcode.cn/problems/binary-tree-right-side-view/">...</a>
 **/
public class 二叉树的右视图 {

    public List<Integer> rightSideView(TreeNode root) {
        if (root == null) return Collections.emptyList();

        if (root.left == null && root.right == null) return Collections.singletonList(root.val);

        ArrayList<Integer> list = new ArrayList<>();

        Deque<TreeNode> queue = new LinkedList<>();
        queue.push(root);

        while (!queue.isEmpty()){

            int size = queue.size();
            for (int i = 0; i < size ; i++){
                TreeNode pollFirst = queue.poll();
                if (i+1==size) list.add(pollFirst.val);
                if (pollFirst.left!=null) queue.offer(pollFirst.left);
                if (pollFirst.right!=null) queue.offer(pollFirst.right);
            }

        }

        return list;
    }
}
